<?php
/**
我的地址
 */


namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MyAddressController extends Controller{

    public function GetMyResident($id){
        $data        = DB::table('myresident')
                     ->leftjoin('user','myresident.user_resident','=','user.user_id')
                     ->where('user_resident',$id)
                     ->get();
        return $data;
    }

    public function MyResident($id){
        $data        = DB::table('myresident')
                     ->where('user_resident',$id)
                     ->count('user_resident');
        return $data;
    }

    //详情
    public function SelectMyAddress(Request $request){
        if($request ->isMethod('post')){
            $myaddress_id    = $request ->input('user_id');
            $data        = DB::table('user')
                       ->leftjoin('myresident','myresident.user_resident','=','user.user_id')
                       ->leftjoin('village','user.user_village','=','village.village_id')
                       ->where('user_resident',$myaddress_id)
                       ->get();
            foreach ($data as $value) {
                $ids               = $value ->user_resident;
                $village_name = $value->village_name;
                $building = $value->floor;
                $element = $value->number;
                $chamber = $value->room;
                $address = $village_name . $building . $element . $chamber;
                $myresident       = $this ->GetMyResident($ids);
                foreach ($myresident as $key){
                    $id       = $key->resident_id;
                    $tmpArray[$id]['resident_id']  =$id;
                    $tmpArray[$id]['head']    = $key->head;
                    $tmpArray[$id]['resident_name']    = $key->resident_name;
                    $tmpArray[$id]['resident_phone']   = $key ->resident_phone;
                    $tmpArray[$id]['resident_type']    = $key ->resident_type;
                    $list =     array_values($tmpArray);
                }
                if(empty($list)){
                    $da = array('address' => $address,'village_id'=>$value->village_id,'village_name'=>$village_name,
                        'building'=>$building,'element'=>$element,'chamber'=>$chamber,'list'=>'');
                }else{
                    $da = array('address' => $address,'village_id'=>$value->village_id,
                        'village_name'=>$village_name,'building'=>$building,'element'=>$element,'chamber'=>$chamber,'list'=>$list);
                }

            }
                if ($data == true) {
                    return json::json('success', '获取详情成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', json::value(1));
                }
            }
    }


    //获取全部地址
    public function AllMyAddress(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('user_id');
            $data        = DB::table('user')
                         ->leftjoin('myresident','myresident.user_resident','=','user.user_id')
                         ->leftjoin('village','user.user_village','=','village.village_id')
                         ->where('user_id',$user_id)
                         ->get();
            foreach ($data as $value){
                $id               = $value ->user_id;
                $village_name     = $value->user_address;
                $name             = $value ->village_name;

/*                $building         = $value ->floor;
                $element          = $value->number;
                $chamber          = $value ->room;*/
                $user_roomname    = $value ->user_roomname;
                $address          = $name.'-'.$user_roomname;
                $myresident       = $this ->MyResident($id);
                $tmpArray[$id]['user_id'] =$id;
                $tmpArray[$id]['address'] = $address;
                $tmpArray[$id]['sum'] = $myresident;
                $da = array_values($tmpArray);
            }
            if ($data == true) {
                return json::json('success', '获取全部地址成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }
    }
    //添加新地址
    public function AddMyAddress(Request $request){
        if($request ->isMethod('post')){
             $user_id     = $request ->input('user_id');   //用户ID
             $village     = $request->input('village_id'); //小区ID
             $name        = $request ->input('name');
             $village_phone       = $request ->input('phone');
             $city                = $request ->input('city');
             $building            = $request ->input('building');
             $element     = $request ->input('element');
             $chamber     = $request ->input('chamber');
             $select      = DB::table('myaddress')
                          ->where('user_myaddress',$user_id)
                          ->where('village_myaddress',$village)
                          ->where('element',$element)
                          ->where('chamber',$chamber)
                          ->get();
             if($select == true){
                 return json::json('failure', '添加新地址已经存在', '');
             }else {
                 $data = DB::table('myaddress')
                     ->insert([
                         'user_myaddress' => $user_id,
                         'village_myaddress' => $village,
                         'name'                =>$name,
                         'myaddress_phone'          =>$village_phone,
                         'myaddress_city'           =>$city,
                         'building'                  =>$building,
                         'element' => $element,
                         'chamber' => $chamber
                     ]);
                 if ($data == true) {
                     return json::json('success', '添加新地址成功', '');
                 } else {
                     return json::json('failure', '由于网络或者不正当原因失败', '');
                 }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //修改新地址
    public function UpdateMyAddress(Request $request){
        if($request ->isMethod('post')){
            $village     = $request->input('village_id'); //小区ID
            $name        = $request ->input('name');
            $village_phone       = $request ->input('phone');
            $city                = $request ->input('city');
            $building            = $request ->input('building');
            $element     = $request ->input('element');
            $chamber     = $request ->input('chamber');
            $myaddress_id  = $request ->input('myaddress_id');

            $data = DB::table('myaddress')
                ->where('myaddress_id',$myaddress_id)
                ->update([
                    'village_myaddress' => $village,
                    'name'                =>$name,
                    'myaddress_phone'          =>$village_phone,
                    'myaddress_city'           =>$city,
                    'building'                  =>$building,
                    'element' => $element,
                    'chamber' => $chamber
                ]);
            if ($data == true) {
                return json::json('success', '修改新地址成功', '');
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }
    }

    //删除新地址

    public function DelMyAddress(Request $request){
        if($request ->isMethod('post')){
            $myaddress_id  = $request ->input('myaddress_id');
            $data = DB::table('myaddress')
                ->where('myaddress_id',$myaddress_id)
                ->delete();
            if ($data == true) {
                return json::json('success', '删除新地址成功', '');
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }
    }


}